package com.mycompany.myapp.service.impl;

import com.mycompany.myapp.domain.StudentScore;
import com.mycompany.myapp.repository.StudentScoreRepository;
import com.mycompany.myapp.service.StudentScoreService;
import com.mycompany.myapp.service.dto.StudentScoreDTO;
import com.mycompany.myapp.service.mapper.StudentScoreMapper;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Service Implementation for managing {@link StudentScore}.
 */
@Service
@Transactional
public class StudentScoreServiceImpl implements StudentScoreService {

    private final Logger log = LoggerFactory.getLogger(StudentScoreServiceImpl.class);

    private final StudentScoreRepository studentScoreRepository;

    private final StudentScoreMapper studentScoreMapper;

    public StudentScoreServiceImpl(StudentScoreRepository studentScoreRepository, StudentScoreMapper studentScoreMapper) {
        this.studentScoreRepository = studentScoreRepository;
        this.studentScoreMapper = studentScoreMapper;
    }

    @Override
    public StudentScoreDTO save(StudentScoreDTO studentScoreDTO) {
        log.debug("Request to save StudentScore : {}", studentScoreDTO);
        StudentScore studentScore = studentScoreMapper.toEntity(studentScoreDTO);
        studentScore = studentScoreRepository.save(studentScore);
        return studentScoreMapper.toDto(studentScore);
    }

    @Override
    public Optional<StudentScoreDTO> partialUpdate(StudentScoreDTO studentScoreDTO) {
        log.debug("Request to partially update StudentScore : {}", studentScoreDTO);

        return studentScoreRepository
            .findById(studentScoreDTO.getId())
            .map(existingStudentScore -> {
                studentScoreMapper.partialUpdate(existingStudentScore, studentScoreDTO);

                return existingStudentScore;
            })
            .map(studentScoreRepository::save)
            .map(studentScoreMapper::toDto);
    }

    @Override
    @Transactional(readOnly = true)
    public Page<StudentScoreDTO> findAll(Pageable pageable) {
        log.debug("Request to get all StudentScores");
        return studentScoreRepository.findAll(pageable).map(studentScoreMapper::toDto);
    }

    @Override
    @Transactional(readOnly = true)
    public Optional<StudentScoreDTO> findOne(Long id) {
        log.debug("Request to get StudentScore : {}", id);
        return studentScoreRepository.findById(id).map(studentScoreMapper::toDto);
    }

    @Override
    public void delete(Long id) {
        log.debug("Request to delete StudentScore : {}", id);
        studentScoreRepository.deleteById(id);
    }
}
